<?php
class create_task_model extends CI_Model {

	public function __construct()
	{
		parent::__construct();
	}
	
	public function add_task($new_task_array, $days_array, $emailphone_array)
	{
		$input['task'] = $new_task_array['task'];
		$input['user_id'] = 7;
		$input['is_email'] = false;
		$input['is_phone'] = false;
		$email = false;
		$phone = false;
		
		foreach ($emailphone_array as $key => $value) {
			if($emailphone_array[$key] == 'email'){
				$email = true;
			}
			if($emailphone_array[$key] == 'phone'){
				$phone = true;
			}
		}
		if($email){
			$input['is_email'] = true;
		}
		if($phone){
			$input['is_phone']= true;
		}
		if($new_task_array['task_type'] == 'nonrecurring')
		{
			$input['reminder_type'] = 'Once';
			$input['start_date'] = $new_task_array['date'];
			$input['end_date'] = $new_task_array['date'];
			$input['reminder_time']= $new_task_array['nrtime'];
		}
		if($new_task_array['task_type'] == 'recurring')
		{
			$input['reminder_type'] = 'Recurring';
			$input['start_date'] = $new_task_array['start_date'];
			$input['end_date'] = $new_task_array['end_date'];
			$input['reminder_time']= $new_task_array['rtime'];
		}
		$status = $this->db->insert('reminder',$input);
		
		if($status == 1){
			if($new_task_array['task_type'] == 'recurring'){
				$this->db->select_max('id');
				$query = $this->db->get('reminder');
				
				$reminder_id =0;
				if($query->num_rows() > 0) {
					foreach ($query->result() as $result) {
						$reminder_id = $result->id;
					}
					$input_days['reminder_id'] = $reminder_id;
					foreach ($days_array as $day){
						$input_days['day_name'] = $day;
						$this->db->insert('reminder_days', $input_days);
					}
					
					// add entries to cron table (scheduled_reminders)
					$schreminders['id'] = $reminder_id;
					$schreminders['scheduled_time'] = $input['reminder_time'];
					$start_date= $input['start_date']; //yyyy-mm-dd
					$end_date= $input['end_date'];
					
					//traverse from start to end
					$sch_date = $start_date;
					//echo "sch date initially: ".$sch_date."<br/>";
					while($end_date>=$sch_date) {
						//for each date find the day and check if it is present in $days_array
						$h = strtotime($sch_date);
						//if it is then insert into scheduled_reminders for that $sch_date else continue until end date is reached.
						$w= date("l", $h) ;
						$w_formatted= substr ( $w , 0, 3);
						if(in_array($w_formatted,$days_array)){
							$check = 1;
						}
						else{
							$check = 0;
						}
						//echo "w_formatted: ".$w_formatted."<br/>";
						//echo "w: ".$w."<br/>";
						//echo "check: ".$check."<br/>";
						if($check == 1){
							//insert in scheduled_reminders
							$schreminders['scheduled_date'] = $sch_date;
							$this->db->insert('scheduled_reminders', $schreminders);
						}
						$sch_date = gmdate('Y-m-d', strtotime("+1 day",$h));
						//echo "sch_date ".$sch_date."<br/>";
						//echo "end_date ".$end_date."<br/>";
					}
					return "pass-r";
				}
				else{
					//reminder id not found 
					return "fail-r-rnf";
				}
			}
			else{
				// add entries to cron table (scheduled_reminders)
				$this->db->select_max('id');
				$query = $this->db->get('reminder');
				
				$reminder_id =0;
				if($query->num_rows() > 0) {
					foreach ($query->result() as $result) {
						$reminder_id = $result->id;
					}
					$schreminders['id'] = $reminder_id;
					$schreminders['scheduled_date'] = $input['start_date'];
					$schreminders['scheduled_time'] = $input['reminder_time'];
					$this->db->insert('scheduled_reminders', $schreminders);
					return "pass-nr";
				}
			}
		}
		else{
			return "fail";
		}
	}
	public function delete($reminder_id)
	{	
		//echo $reminder_id;
		// 1. find the reminder type
		$this->db->select('reminder_type'); #Because I need the value
		$this->db->where('id', $reminder_id); #Because I need the variable column entitled siteoverview
		$query = $this->db->get("reminder"); #From the settings table
		
		$row = $query->result(); // get the row
		
		$reminder_type = $row[0]->reminder_type;
		
		// 2. if recurring,
		if($reminder_type == 'Recurring'){
			// 		2.1 delete from reminder_days
			//		2.2 delete from scheduled_reminders
			//		2.3 delete from reminder
			$this->db->delete('reminder_days', array('reminder_id' => $reminder_id)); 
			$this->db->delete('scheduled_reminders', array('id' => $reminder_id)); 
			$this->db->delete('reminder', array('id' => $reminder_id)); 
			return "Deleted Reminder";
		}

		//	3. if non recurring
		else if($reminder_type == 'Once'){
			$this->db->delete('scheduled_reminders', array('id' => $reminder_id)); 
			$this->db->delete('reminder', array('id' => $reminder_id)); 
			return "Deleted Reminder";
		}
		//		3.1 delete from scheduled_reminders
		//		3.2 delete from reminder
		
	}
	
	public function edit($reminder_id)
	{	
		echo $reminder_id;
		
	}
	
	public function get_task_list($userid)
	{
		/*
			1. go to reminder table, get all non recurring reminders for the given user id
		*/
		$this->db->where('user_id',$userid);
		$this->db->where('reminder_type','Once');
		$query = $this->db->get('reminder');
		
		$results_array['r'] = array();
		$results_array['nr'] = array();
		$results_array['days_array'] = array();
		
		/*  2. construct an array of records and send to controller
		*/
		if($query->num_rows() > 0) {
			$results_array['nr'] = $query->result() ;
		}
		
		/*	3. go to reminder table, get all recurring reminders for the given user id
		*/		
		$this->db->where('user_id',$userid);
		$this->db->where('reminder_type','Recurring');
		$query = $this->db->get('reminder');
		
		
		/*  3.1 get the details from reminder_days table for the recurring reminder id.
		*/	
		if($query->num_rows() > 0) {
			$results_array['r'] = $query->result() ;
			
			foreach ($query->result() as $result) {
				$reminder_id = $result->id;
				$this->db->where('reminder_id',$reminder_id);
				$days_array[$reminder_id]=$this->db->get('reminder_days');
			}
			/* 4. construct an array of records and send to controller 
			*/
			$results_array['days_array'] = $days_array;
		}
		
		return $results_array;
	}
}
?>
